home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / save pict file / savepictfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.4 KB  |  143 lines

  1. /*
  2.     File:        SavePictFile.c
  3.  
  4.     Contains:    Save a PICT file:  Creates a QuickDraw PICT and saves it as a PICT file,
  5.                 including the required header of 512 bytes of nothing important.
  6.  
  7.  
  8.     Written by:     
  9.  
  10.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */ 
  25. #include <Files.h>
  26. #include <StandardFile.h>
  27. #include <Fonts.h>
  28.  
  29.  
  30.  
  31. PicHandle InitPicture(void);
  32. void PutPictToFile(PicHandle thePicture);
  33.  
  34. /*------ main ----------------------------------------------------------------------------*/
  35.  
  36. PicHandle ourPict;
  37.  
  38. void main()
  39. {
  40.  
  41.     GrafPort myPort;
  42.  
  43.  
  44.     InitGraf(&qd.thePort);
  45.     OpenPort(&myPort);
  46.     InitFonts();
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(nil);
  51.     InitCursor();
  52.  
  53.       ourPict = InitPicture();                       // get our picture
  54.       PutPictToFile(ourPict);                        // put it to a file
  55.       KillPicture(ourPict);                       // and then kill it
  56.  
  57. } /* main */
  58.  
  59.  
  60.  
  61. /*------ PutPictToFile ----------------------------------------------------------------------------*/
  62.  
  63.  
  64. void PutPictToFile(PicHandle thePicture)
  65.  
  66. {
  67.  
  68.     SFReply    tr;
  69.     short    fRefNum, count;
  70.     long inOutCount;
  71.     Point    where;
  72.     unsigned char header[512];
  73.     OSErr myError;
  74.     
  75.     for (count = 0; count < 512; count++)
  76.         header[count] = 0x00;
  77.     
  78.     where.h=100; where.v=50;     /* where the  Standard File dialog window goes */
  79.     
  80.     SFPutFile( where, "\pSave PICT as:", "\pMy PICT File", NULL, &tr );
  81.     
  82.     if ( tr.good ) {
  83.         myError = Create(tr.fName, tr.vRefNum, '????', 'PICT');
  84.         if (myError == dupFNErr) {
  85.             myError = FSDelete(tr.fName,tr.vRefNum);
  86.             myError = Create(tr.fName, tr.vRefNum, '????', 'PICT');
  87.         }        /* this is quick 'n' dirty or there'd be more robust handling here */
  88.         
  89.         myError = FSOpen( tr.fName, tr.vRefNum, &fRefNum );
  90.         if ( myError == noErr ) { 
  91.             inOutCount = 512;
  92.             myError = FSWrite(fRefNum, &inOutCount, header);        /* write the header */
  93.             HLock((Handle)thePicture);
  94.             if (myError == noErr) {                    /* don't write if error the first time */
  95.                 inOutCount = GetHandleSize((Handle)thePicture);
  96.                 myError = FSWrite(fRefNum,&inOutCount,*thePicture);
  97.             }
  98.             FSClose( fRefNum );            /* close it */
  99.             HUnlock((Handle)thePicture);
  100.         }
  101.     }
  102. }
  103.  
  104.  
  105.  
  106. /*------ InitPicture ----------------------------------------------------------------------*/
  107.  
  108. PicHandle InitPicture (void)
  109.  
  110. {
  111.     Rect myRect;
  112.     PicHandle thePicHandle;
  113.     CGrafPort myPort;
  114.     PixPatHandle thePixPat;
  115.     short theFont, textSize = 14;
  116.     
  117.     OpenCPort(&myPort);
  118.     SetRect(&myRect,0,0,200,200);
  119.     thePicHandle = OpenPicture(&myRect);
  120.     ClipRect(&myRect);
  121.     
  122.     thePixPat = GetPixPat(128);
  123.     
  124.     FillCOval(&myRect,thePixPat);
  125.     
  126.     MoveTo(22,22);
  127.     LineTo(55,55);
  128.     LineTo(58,22);
  129.     LineTo(22,58);
  130.     
  131.     GetFNum ("\pTimes", &theFont);
  132.     TextFont (theFont);
  133.     TextSize (textSize);
  134.     
  135.     DrawString("\pA wonderful test");
  136.     
  137.     ClosePicture();
  138.     CloseCPort(&myPort);
  139.     
  140.     return(thePicHandle);
  141.         
  142. }  /* InitPicture */
  143.